package com.ljh.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ljh.StudentVo;
import com.ljh.pojo.Student;
import com.ljh.pojo.StudentTag;
import com.ljh.service.IStudentService;
import com.ljh.service.IStudentTagService;
import com.ljh.service.ITagService;
import com.ljh.service.impl.TagServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lijiahao
 * @since 2021-09-25
 */
@RestController
@RequestMapping("/student")
public class StudentController {


    @Autowired
    private IStudentService studentService;
    @Autowired
    private IStudentTagService studentTagService;
    @Autowired
    private ITagService tagService;

    @GetMapping
    public List<StudentVo> listAll(){
        List<Student> list = studentService.list();
        ArrayList<StudentVo> studentVos = new ArrayList<>();
        for (Student student : list) {
            StudentVo vo = new StudentVo();
            BeanUtils.copyProperties(student,vo);
            List<StudentTag> tags = studentTagService.list(new QueryWrapper<StudentTag>().eq("sid", student.getId()));
            List<Integer> tids = tags.stream().map(tag -> tag.getTid()).collect(Collectors.toList());
            vo.setTags(tagService.listByIds(tids));
            studentVos.add(vo);
        }

        return studentVos;
    }
}
